/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 * A class to load and store data in the J2SE Properties format.
 * <p>
 * The Properties are similar to the one found on the j2se platform
 * </p>
 * 
 */
public class Properties {
	Hashtable data = new Hashtable();

	public Properties(InputStream is) {
		load(is);
	}

	public Properties(String path) {
		load(getClass().getResourceAsStream(path));
	}

	/**
	 * Searches for the property with the specified key in this property list.
	 * The method returns null if the property is not found.
	 * 
	 * @param key
	 *            - the property key.
	 * @return the value in this property list with the specified key value.
	 */
	public String get(String key) {
		return (String) data.get(key);
	}

	/**
	 * Searches for the property with the specified key in this property list.
	 * The method returns the default value argument if the property is not
	 * found.
	 * 
	 * @param key
	 *            - the property key.
	 * @param defaultValue
	 *            - a default value.
	 * @return the value in this property list with the specified key value.
	 */
	public String getProperty(String key, String defaultValue) {
		String value = this.get(key);

		if (value == null)
			value = defaultValue;

		return value;
	}

	/**
	 * Does not support comments in the properties file
	 * <p>
	 * The '=' char is used to separate key from value.<br>
	 * Uses utf-8 encoding.
	 * 
	 * </p>
	 * 
	 * @param is
	 */
	private void load(InputStream is) {
		try {
			InputStreamReader reader = new InputStreamReader(is, "UTF-8");
			int c = 0;
			while (c != -1) {
				StringBuffer buffer = new StringBuffer();
				do {
					c = reader.read();
					if (c == '\r') {
						continue;
					}
					if (c == -1 || c == '\n') {
						break;
					}
					buffer.append((char) c);
				} while (true);
				String string = buffer.toString();
				int index = string.indexOf('=');
				if (index < 0) {
					continue;
				}
				String key = string.substring(0, index);
				String value = string.substring(index + 1);
				data.put(key, value);
			}
			System.out.println(data.size() + " Properties found!");
		} catch (IOException e) {
			System.out.println("Could not load property " + e.toString());
		}

	}

	/**
	 * Writes this property list (key and element pairs) in this Properties
	 * table to the output stream in a format suitable for loading into a
	 * Properties table using the load method.
	 * 
	 * @param out
	 *            - an output stream.
	 * @param data
	 * 
	 * @throws IOException
	 *             - if writing this property list to the specified output
	 *             stream throws an IOException.
	 */
	public void store(OutputStream out, Hashtable data) throws IOException {

		// getting a list of keys
		Enumeration eKeys = data.keys();

		// writing keys and values
		while (eKeys.hasMoreElements()) {
			String key = (String) eKeys.nextElement();
			out.write((key + "=" + data.get(key) + "\r\n").getBytes());
		}
	}
}
